home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’92 / PatchWorks Kit / <PatchWorks++> / TestExtension.c < prev    next >
C/C++ Source or Header  |  1992-04-21  |  5KB  |  237 lines

  1. /*
  2.     TestExtension.c
  3.     
  4.     TestBed for developing extensions.
  5.     
  6.     Pretends to be an INIT and calls Install().
  7.     
  8.     by Mouse Herrell & Patrick Beard.
  9.     
  10.     © 1991 Berkeley Systems Inc.
  11. */
  12.  
  13. #include <Windows.h>
  14. #include <GestaltEqu.h>
  15. #include <Notification.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. #include "ScalperPatch.h"
  20. #include "TestExtension.h"
  21. #include "Exceptions.h"
  22.  
  23. enum {
  24.     eAppleID = 1,
  25.     eFileID,
  26.     eEditID
  27. };
  28.  
  29. enum {
  30.     eQuitItem = 1
  31. };
  32.  
  33. void ExitTask(void);
  34. void HandleMenu(long mSelect);
  35. void ReportError(OSErr err, char* stage);
  36.  
  37. static MenuHandle theAppleMenu, theFileMenu, theEditMenu;
  38. static short theFamily = -1;
  39. static Boolean theInstalledFlag = false;
  40.  
  41. short theSystemVersion;
  42. Boolean theCQDFlag;
  43.  
  44. OSErr main(void)
  45. {
  46.     OSErr err;
  47.     static char* stage;
  48.     SysEnvRec env;
  49.     
  50.     SysEnvirons(1, &env);
  51.     theSystemVersion = env.systemVersion;
  52.     theCQDFlag = env.hasColorQD;
  53.     MaxApplZone();
  54.     try {
  55.         long        pval;
  56.         
  57.         stage = "installing the cleanup task";
  58.         err = _atexit(ExitTask);
  59.         if (err)
  60.             throw (err);
  61.         stage = "requesting the Scalper hook";
  62.         ScalperPatch::InstallScalper();
  63.         InitGraf(&thePort);
  64.         stage = "calling Install()";
  65.         theInstalledFlag = true;
  66.         Install();
  67.     } catch {
  68.         err = theException;
  69.         ReportError(err, stage);
  70.     }
  71.     try {
  72.         BitMap    map = { 0, 4, { 16, 80, 48, 112 } };
  73.         
  74.         stage = "initializing the Toolbox";
  75.         InitGraf(&thePort);
  76.         InitCursor();
  77.         InitFonts();
  78.         InitWindows();
  79.         InitMenus();
  80.         TEInit();
  81.         InitDialogs(0);
  82.         FlushEvents(everyEvent, 0);
  83.         stage = "creating the menus";
  84.         InsertMenu(theAppleMenu = NewMenu(eAppleID, "\p\024"), 0);
  85.         InsertMenu(theFileMenu = NewMenu(eFileID, "\pFile"), 0);
  86.         InsertMenu(theEditMenu = NewMenu(eEditID, "\pEdit"), 0);
  87.         DrawMenuBar();
  88.         AddResMenu(theAppleMenu, 'DRVR');
  89.         AppendMenu(theFileMenu, "\pQuit/Q");
  90.         AppendMenu(theEditMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  91.         if (theFamily != -1) {
  92.             WindowPtr    window = 0;
  93.             Rect        r = { 64, 64, 128, 256 };
  94.             Handle        icon = GetResource('ICN#', theFamily);
  95.             
  96.             stage = "loading the icon";
  97.             if (!icon) {
  98.                 OSErr    err = ResError();
  99.                 if (err == noErr)
  100.                     err = memFullErr;
  101.                 throw (err);
  102.             }
  103.             HLock(icon);
  104.             map.baseAddr = *icon;
  105.             stage = "allocating the window record";
  106.             window = NewWindow(NULL, &r, CurApName, true, movableDBoxProc, NULL, false, 0);
  107.             if (!window)
  108.                 throw (memFullErr);
  109.             SetPort(window);
  110.         }
  111.         stage = "in the event loop";
  112.         while (1) {
  113.             EventRecord    theEvent;
  114.         
  115.             if (WaitNextEvent(everyEvent, &theEvent, 0, nil)) {
  116.                 switch (theEvent.what) {
  117.                     WindowPtr    theWindow;
  118.                     int            windowCode;
  119.                     
  120.                 case mouseDown:
  121.                     windowCode = FindWindow(theEvent.where, &theWindow);
  122.                     
  123.                     switch (windowCode) {
  124.                         Rect        dragRect;
  125.                         
  126.                     case inSysWindow: 
  127.                         SystemClick(&theEvent, theWindow);
  128.                         break;
  129.                         
  130.                     case inMenuBar:
  131.                         HandleMenu(MenuSelect(theEvent.where));
  132.                         break;
  133.                         
  134.                     case inDrag:
  135.                         dragRect = screenBits.bounds;
  136.                         dragRect.top = GetMBarHeight();
  137.                         InsetRect(&dragRect, 4, 4);
  138.                         DragWindow(theWindow, theEvent.where, &dragRect);
  139.                         break;
  140.                         
  141.                     case inContent:
  142.                         if (theWindow != FrontWindow())
  143.                             SelectWindow(theWindow);
  144.                         break;
  145.                     
  146.                     case inGoAway:
  147.                         if (TrackGoAway(theWindow, theEvent.where))
  148.                             ExitToShell();
  149.                         break;
  150.                     }
  151.                     break;
  152.                     
  153.                 case keyDown: 
  154.                 case autoKey:
  155.                     if ((theEvent.modifiers & cmdKey) != 0)
  156.                         HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  157.                     break;
  158.                     
  159.                 case updateEvt:
  160.                     theWindow = (WindowPtr)theEvent.message;
  161.                     BeginUpdate(theWindow);
  162.                     if (theFamily != -1) {
  163.                         CopyBits(&map, &theWindow->portBits, &map.bounds, &map.bounds,
  164.                                 srcCopy, theWindow->visRgn);
  165.                     }
  166.                     EndUpdate(theWindow);
  167.                     break;
  168.                     
  169.                 case activateEvt:
  170.                     break;
  171.                 }
  172.                 HiliteMenu(0);
  173.             }
  174.         }
  175.     } catch {
  176.         static    char    message[256];
  177.         
  178.         err = theException;
  179.         sprintf(message, "An error of type %hd occurred while %s.", err, stage);
  180.         CtoPstr(message);
  181.         DebugStr(message);
  182.     }
  183. }
  184.  
  185. void ReportError(OSErr err, char* stage)
  186. {
  187.     static    char    message[256];
  188.     static    NMRec    notification = { 0, nmType };
  189.     
  190.     notification.nmMark = 0;
  191.     notification.nmIcon = 0;
  192.     notification.nmSound = (void*)-1;
  193.     sprintf(message, "An error of type %hd occurred while %s.", err, stage);
  194.     notification.nmStr = c2pstr(message);
  195.     notification.nmResp = (void*)-1;
  196.     NMInstall(¬ification);
  197. }
  198.  
  199. void HandleMenu(long mSelect)
  200. {
  201.     int            menuID = HiWord(mSelect);
  202.     int            menuItem = LoWord(mSelect);
  203.     Str255        name;
  204.     GrafPtr        savePort;
  205.     WindowPeek    frontWindow;
  206.     
  207.     switch (menuID) {
  208.     case eAppleID:
  209.         GetPort(&savePort);
  210.         GetItem(theAppleMenu, menuItem, name);
  211.         OpenDeskAcc(name);
  212.         SetPort(savePort);
  213.         break;
  214.     
  215.     case eFileID:
  216.         if (menuItem == eQuitItem)
  217.             ExitToShell();
  218.         break;
  219.                 
  220.     case eEditID:
  221.         if (!SystemEdit(menuItem-1))
  222.             SysBeep(5);
  223.     }
  224. }
  225.  
  226. void ExitTask(void)
  227. {
  228.     if (theInstalledFlag)
  229.         Remove();
  230.     ScalperPatch::RemoveScalper();
  231. }
  232.  
  233. void ShowIconFamily(short iconId)
  234. {
  235.     theFamily = iconId;
  236. }
  237.